package com.cheyun.netsalev3.adapter

import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import java.io.Serializable


/**
 * 二级列表抽象类
 */

abstract class SecondaryListAdapter<GVH : RecyclerView.ViewHolder, SVH : RecyclerView.ViewHolder> : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    lateinit var context: Context
    private val groupItemStatus = ArrayList<Boolean>()

    private var dataTrees: List<DataTree<*, *>> = ArrayList()

    /**
     * 为适配器设置新数据以显示。在设置新数据时必须调用它。
     */
    fun notifyNewData(data: List<DataTree<*, *>>) {
        setDataTrees(data)
    }

    /**
     * 为适配器设置新数据并通知更改。
     */
    private fun setDataTrees(dt: List<DataTree<*, *>>) {
        this.dataTrees = dt
        initGroupItemStatus(groupItemStatus)
        notifyDataSetChanged()
    }

    /**
     * 默认二级列表关闭
     *
     */
    private fun initGroupItemStatus(l: ArrayList<Boolean>) {
        for (i in dataTrees.indices) {
            l.add(i, false)
        }
    }

    /**
     * 头部ViewHolder
     */
    abstract fun groupItemViewHolder(parent: ViewGroup): RecyclerView.ViewHolder

    /**
     * 子选项ViewHolder
     */
    abstract fun subItemViewHolder(parent: ViewGroup): RecyclerView.ViewHolder

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        context = parent.context
        lateinit var viewHolder: RecyclerView.ViewHolder
        if (viewType == ItemStatus.VIEW_TYPE_GROUPITEM) {
            viewHolder = groupItemViewHolder(parent)
        } else if (viewType == ItemStatus.VIEW_TYPE_SUBITEM) {
            viewHolder = subItemViewHolder(parent)
        }
        return viewHolder
    }

    /**
     * 更新指定组项的内容。这个方法将由OnbndIdWistor调用。
     */
    abstract fun onGroupItemBindViewHolder(data: Any?, groupItemIndex: GVH, groupItemIndex1: Int)

    /**
     * 更新指定子项的内容。这个方法将由OnbndIdWistor调用。
     *
     */
    abstract fun onSubItemBindViewHolder(data: Any?, holder: SVH, groupItemIndex: Int, subItemIndex: Int)

    /**
     * 当单击组项时将调用该方法。
     *
     */
    abstract fun onGroupItemClick(isExpand: Boolean, holder: GVH, groupItemIndex: Int)

    /**
     * 单击子项时将调用该方法。
     *
     */
    abstract fun onSubItemClick(holder: SVH, groupItemIndex: Int, subItemIndex: Int)

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val itemStatus = getItemStatusByPosition(position)
        val dt = dataTrees[itemStatus.groupItemIndex]
        if (itemStatus.viewType == ItemStatus.VIEW_TYPE_GROUPITEM) {
            onGroupItemBindViewHolder(dt.groupItem, holder as GVH, itemStatus.groupItemIndex)
            holder.itemView.setOnClickListener {
                val groupItemIndex = itemStatus.groupItemIndex
                if (!groupItemStatus[groupItemIndex]) {
                    onGroupItemClick(false, holder as GVH, groupItemIndex)
                    groupItemStatus[groupItemIndex] = true
                    notifyItemRangeInserted(holder.adapterPosition + 1, dt.subItems.size)
                } else {
                    onGroupItemClick(true, holder as GVH, groupItemIndex)
                    groupItemStatus[groupItemIndex] = false
                    notifyItemRangeRemoved(holder.adapterPosition + 1, dt.subItems.size)
                }
            }


        } else if (itemStatus.viewType == ItemStatus.VIEW_TYPE_SUBITEM) {
            onSubItemBindViewHolder(dt.subItems[itemStatus.subItemIndex], holder as SVH, itemStatus.groupItemIndex, itemStatus
                    .subItemIndex)
            holder.itemView.setOnClickListener { onSubItemClick(holder as SVH, itemStatus.groupItemIndex, itemStatus.subItemIndex) }
        }
    }

    fun setDefaultShow(index:Int){
        if(groupItemStatus.size>=index+1){
            groupItemStatus[index] = true
        }
    }


    override fun getItemCount(): Int {
        var itemCount = 0
        if (groupItemStatus.size == 0) {
            return 0
        }
        for (i in dataTrees.indices) {
            if (groupItemStatus[i]) {
                itemCount += dataTrees[i].subItems.size + 1
            } else {
                itemCount++
            }
        }
        return itemCount
    }

    override fun getItemViewType(position: Int): Int {
        return getItemStatusByPosition(position).viewType
    }

    /**
     *获取项目的状态包括视图类型、组项索引和子项索引。
     */
    private fun getItemStatusByPosition(position: Int): ItemStatus {
        val itemStatus = ItemStatus()
        var count = 0
        var i = 0
        i = 0
        while (i < groupItemStatus.size) {
            if (count == position) {
                itemStatus.viewType = ItemStatus.VIEW_TYPE_GROUPITEM
                itemStatus.groupItemIndex = i
                break
            } else if (count > position) {
                itemStatus.viewType = ItemStatus.VIEW_TYPE_SUBITEM
                itemStatus.groupItemIndex = i - 1
                itemStatus.subItemIndex = position - (count - dataTrees[i - 1].subItems.size)
                break
            }
            count++
            if (groupItemStatus.get(i)) {
                count += dataTrees[i].subItems.size
            }
            i++
        }
        if (i >= groupItemStatus.size) {
            itemStatus.groupItemIndex = i - 1
            itemStatus.viewType = ItemStatus.VIEW_TYPE_SUBITEM
            itemStatus.subItemIndex = position - (count - dataTrees[i - 1].subItems.size)
        }
        return itemStatus
    }

    private class ItemStatus {
        var viewType: Int = 0
        var groupItemIndex = 0
        var subItemIndex = -1
        companion object {
            val VIEW_TYPE_GROUPITEM = 0
            val VIEW_TYPE_SUBITEM = 1
        }
    }

    /**
     * 封装数据
     */
    class DataTree<K, V>(val groupItem: K, val subItems: List<V>) : Serializable
}